(function() {
	var using = NEJ.P;
	var o = NEJ.O;
	var e = using("nej.e");
	var t = using("nej.ut");
	var u = using("nej.u");
	var v = using("nej.v");
	var p = using("nej.ui");
	var j = using("nej.j");
	var dd = using("dd.data");
	var m = using("dd.module");
	var di = using("dd.ui");
	var du = using("dd.util");
	var dm = using("dd.message");
	var dc = using("dd.chat");
	var dw = using("dd.widget");
	var dataViewer = {
		init: function() {
			this.accoutCache = dd._$$AccountCache._$allocate();
			this.initNode();
			this.initEvent();
			this.initData();
			this.initHdmsg2();
			this.initReport();
			this.initLotterymsg();
			this.initReplyalarm()
		},
		initNode: function() {
			this.contact = e._$get("contact-statistic");
			this.message = e._$get("message-statistic")
		},
		initData: function() {
			this.__endDate = (new Date).setTimeByStr(LATEST);
			e._$get("endTime").value = LATEST;
			this.__startDate = new Date((new Date).setTimeByStr(LATEST).getTime() - 7 * 24 * 60 * 60 * 1e3);
			e._$get("startTime").value = this.__startDate.format("yyyy-MM-dd");
			this.__getIncrement();
			this.__getContact();
			this.__getMessage()
		},
		__getIncrement: function() {
			j._$requestByREST("/statistics/increment", {
				method: "GET",
				onload: function(data) {
					var dataSet = data.result;
					this.__fillData("increase", dataSet.addnew);
					this.__fillData("quit", dataSet.cancel);
					this.__fillData("total", dataSet.total)
				}.bind(this),
				onerror: function(data) {}
			})
		},
		__getContact: function() {
			this.showLoading(this.contact);
			j._$requestByREST("/statistics/user/send", {
				method: "GET",
				param: {
					starttime: e._$get("startTime").value,
					endtime: e._$get("endTime").value,
					country: 0
				},
				onload: this.cbContact.bind(this)
			})
		},
		__getMessage: function() {
			this.showLoading(this.message);
			j._$requestByREST("/statistics/message/area", {
				method: "GET",
				param: {
					starttime: e._$get("startTime").value,
					endtime: e._$get("endTime").value,
					country: 0
				},
				onload: this.cbMessage.bind(this)
			})
		},
		__fillData: function() {
			function addDiv(num) {
				var tmp = num.toString();
				var rest = "";
				var index = tmp.indexOf(".");
				var strArr = [];
				if (index > -1) {
					rest = tmp.substring(index);
					tmp = tmp.substring(0, index)
				}
				switch (tmp.length % 3) {
				case 0:
					for (var i = 0; i * 3 < tmp.length; i++) {
						var segment = tmp.substring(i * 3, (i + 1) * 3);
						if (segment) {
							strArr.push(segment)
						}
					}
					break;
				case 1:
					strArr.push(tmp.substring(0, 1));
					for (var i = 0; 1 + i * 3 < tmp.length; i++) {
						var segment = tmp.substring(1 + i * 3, 1 + (i + 1) * 3);
						if (segment) {
							strArr.push(segment)
						}
					}
					break;
				case 2:
					strArr.push(tmp.substring(0, 2));
					for (var i = 0; 2 + i * 3 < tmp.length; i++) {
						var segment = tmp.substring(2 + i * 3, 2 + (i + 1) * 3);
						if (segment) {
							strArr.push(segment)
						}
					}
					break
				}
				return strArr.join(",") + rest
			}
			function dataTransfor(num, isValid, period, id) {
				var ele = e._$getByClassName(id, "j-" + period)[0];
				if (isValid) {
					if (num > 0) {
						ele.innerHTML = addDiv(num) + "%" + '<div class="increase"></div>'
					} else if (num == 0) {
						ele.innerText = "0" + "%"
					} else {
						ele.innerHTML = addDiv(Math.abs(num)) + "%" + '<div class="increase increase-1"></div>'
					}
				} else {
					if (num > 0) {
						ele.innerHTML = '--<div class="increase"></div>'
					} else if (num == 0) {
						ele.innerText = "--"
					} else {
						ele.innerHTML = '--<div class="increase increase-1"></div>'
					}
				}
			}
			return function(id, dataSet) {
				e._$getByClassName(id, "j-num")[0].innerText = dataSet.num;
				dataTransfor(dataSet.incrPesentageDay, dataSet.dayValid, "day", id);
				dataTransfor(dataSet.incrPesentageWeek, dataSet.weekValid, "week", id);
				dataTransfor(dataSet.incrPesentageMonth, dataSet.monthValid, "month", id)
			}
		} (),
		initEvent: function() {
			v._$addEvent("refresh", "click", this.__refresh.bind(this));
			v._$addEvent("startTime", "click", this.__showDatePicker.bind(this, "start"));
			v._$addEvent("endTime", "click", this.__showDatePicker.bind(this, "end"));
			v._$addEvent("exportRead", "click",
			function() {
				location.href = "/statistics/message/area/download?" + "starttime=" + e._$get("startTime").value + "&endtime=" + e._$get("endTime").value + "&filename=公众号查看次数" + "&country=0" + "&valuetype=msgview"
			});
			v._$addEvent("exportSend", "click",
			function() {
				location.href = "/statistics/user/send/download?" + "starttime=" + e._$get("startTime").value + "&endtime=" + e._$get("endTime").value + "&filename=回复消息次数" + "&country=0" + "&valuetype=sentTimeNum"
			});
			if (e._$get("j-knowbtn")) {
				v._$addEvent(e._$get("j-knowbtn"), "click", this.onClickKnow._$bind(this))
			}
			if (e._$get("j-tipclose")) {
				v._$addEvent(e._$get("j-tipclose"), "click", this.onClickTipClose._$bind(this))
			}
		},
		__refresh: function() {
			if (this.__validateTime()) {
				this.__getContact();
				this.__getMessage()
			}
		},
		__showDatePicker: function(tag, event) {
			v._$stop(event);
			var _input = event.target;
			if (tag == "start") {
				if (!this.__startDatePicker) {
					this.__startDatePicker = p._$$DatePick._$allocate({
						parent: _input.parentNode,
						date: _input.value,
						clazz: "m-dt",
						onchange: this.__onChangeDate.bind(this, _input, tag)
					})
				} else {
					this.__startDatePicker._$show()
				}
				if (this.__endDatePicker) {
					this.__endDatePicker._$hide()
				}
			} else {
				if (!this.__endDatePicker) {
					this.__endDatePicker = p._$$DatePick._$allocate({
						parent: _input.parentNode,
						date: _input.value,
						clazz: "m-dt",
						onchange: this.__onChangeDate.bind(this, _input, tag)
					})
				} else {
					this.__endDatePicker._$show()
				}
				if (this.__startDatePicker) {
					this.__startDatePicker._$hide()
				}
			}
		},
		__onChangeDate: function(input, tag, value) {
			if (tag == "start") {
				this.__startDate = value
			} else {
				this.__endDate = value
			}
			input.value = value.format("yyyy-MM-dd")
		},
		__validateTime: function() {
			if (this.__startDate.getTime() > this.__endDate.getTime()) {
				du.showError("开始时间不能大于结束时间！");
				return false
			}
			if (this.__endDate.getTime() - this.__startDate.getTime() > 1e3 * 60 * 60 * 24 * 30) {
				du.showError("时间段不能大于30天！");
				return false
			}
			return true
		},
		initLotterymsg: function() {
			du.toggleByCookie({
				node: "hdmsg-custommenu",
				cookie: {
					name: "msgCustommenuClose"
				},
				closer: du.get("close", "hdmsg-custommenu")
			})
		},
		initReplyalarm: function() {
			du.toggleByCookie({
				node: "j-replyalarm",
				cookie: {
					name: "replyalarmClose",
					expires: 1
				},
				closer: du.get("close", "j-replyalarm")
			})
		},
		initHdmsg2: function() {
			du.toggleByCookie({
				node: "hdmsg2",
				cookie: {
					name: "hdmsg2Close"
				},
				closer: du.get("close", "hdmsg2")
			})
		},
		initReport: function() {
			du.toggleByCookie({
				node: "hdmsg-report",
				cookie: {
					name: "isReported"
				},
				closer: du.get("close", "hdmsg-report")
			})
		},
		onClickKnow: function() {
			this.accoutCache._$addEvent("onNoteLoad", this.onNoteLoad._$bind(this));
			this.accoutCache._$setNote()
		},
		onNoteLoad: function(data) {
			if (data.code == 200) {
				e._$remove(e._$get("j-notebox"))
			}
		},
		onClickTipClose: function() {
			this.accoutCache._$addEvent("onNoticedLoad", this.onNoticedLoad._$bind(this));
			this.accoutCache._$authenNoticed()
		},
		onNoticedLoad: function(data) {
			if (data.code == 200) {
				e._$remove(e._$get("j-authenTip"))
			}
		},
		cbContact: function(json) {
			var parent = this.contact;
			if (json && json.code == 200) {
				var result = json && json.result;
				var max = 0;
				var ys = result.list.map(function(x) {
					if (x.sentTimeNum > max) {
						max = x.sentTimeNum
					}
					return x.sentTimeNum
				});
				var width = (max.toString().length + 1) * 10;
				var maxWidth = width > 50 ? width: 50;
				var xs = result.list.map(function(x) {
					return (new Date(x.time)).format("MM-dd")
				});
				this.clear(parent);
				var option1 = {
					legend: {
						enabled: false
					},
					tooltip: {
						listener: function(dataMap) {
							var formatter = '<div style="padding:5px 15px;border:2px solid #ebebeb;color:#000;background:#fff;' + "background:rgba(255,255,255,0.7);border-radius:5px;border-color:#8c8c8c;" + 'font:12px/24px arial;">' + dataMap[0].xValue + "";
							for (var i = 0,
							l = dataMap.length; i < l; i++) {
								formatter += "</br>" + dataMap[i].name + "：" + dataMap[i].yValue
							}
							formatter += "</div>";
							return formatter
						},
						strokeColor: "rgba(255,255,255,0)"
					},
					cartesian: {
						left: maxWidth,
						xAxis: {
							space: false,
							color: "#000000",
							labels: xs
						},
						yAxis: {
							color: "#000000",
							lineWidth: .1,
							strokeColor: "#000000"
						}
					},
					series: [{
						type: "line",
						name: "回复消息次数",
						lineWidth: 2,
						symbol: {
							type: "circle",
							lineWidth: 1,
							radius: 2
						},
						data: ys
					}]
				};
				nevCharts("contact-statistic", option1, "/res/swf/")
			} else {
				this.showMessage(parent)
			}
		},
		cbMessage: function(json) {
			var parent = this.message;
			if (json && json.code == 200) {
				var result = json && json.result;
				var max = 0;
				var ys = result.list.map(function(x) {
					if (x.msgview > max) {
						max = x.msgview
					}
					return x.msgview
				});
				var width = (max.toString().length + 1) * 10;
				var maxWidth = width > 50 ? width: 50;
				var xs = result.list.map(function(x) {
					return (new Date(x.time)).format("MM-dd")
				});
				this.clear(parent);
				var option1 = {
					legend: {
						enabled: false
					},
					tooltip: {
						listener: function(dataMap) {
							var formatter = '<div style="padding:5px 15px;border:2px solid #ebebeb;color:#000;background:#fff;' + "background:rgba(255,255,255,0.7);border-radius:5px;border-color:#8c8c8c;" + 'font:12px/24px arial;">' + dataMap[0].xValue + "";
							for (var i = 0,
							l = dataMap.length; i < l; i++) {
								formatter += "</br>" + dataMap[i].name + "：" + dataMap[i].yValue
							}
							formatter += "</div>";
							return formatter
						},
						strokeColor: "rgba(255,255,255,0)"
					},
					cartesian: {
						left: maxWidth,
						xAxis: {
							space: false,
							color: "#000000",
							labels: xs
						},
						yAxis: {
							color: "#000000",
							lineWidth: .1,
							strokeColor: "#000000"
						}
					},
					series: [{
						type: "line",
						name: "公众号查看次数",
						lineWidth: 2,
						symbol: {
							type: "circle",
							lineWidth: 1,
							radius: 2
						},
						data: ys
					}]
				};
				nevCharts("message-statistic", option1, "/res/swf/")
			} else {
				this.showMessage(parent)
			}
		},
		getDays: function(total, timestamp) {
			var total = -total || -7;
			var days = [];
			for (var i = total; i <= -1; i++) {
				days.push(this.getDay(i, timestamp))
			}
			return days
		},
		getDay: function(offset, timestamp) {
			var t = timestamp ? new Date(timestamp) : new Date;
			t.setDate(t.getDate() + offset);
			var y = t.getFullYear();
			var m = t.getMonth() + 1;
			var d = t.getDate();
			return m + "-" + d
		},
		dataFormat: function(xs, ys) {
			var xy = [];
			u._$forEach(xs,
			function(x, index, that) {
				xy.push([x, ys[index]])
			});
			return [{
				data: xy
			}]
		},
		showLoading: function(parent) {
			var evt = {};
			du.showListLoading(evt);
			parent.innerHTML = evt.value
		},
		showMessage: function(parent) {
			var evt = {};
			du.showListMessage("数据加载失败！", evt);
			parent.innerHTML = evt.value
		},
		clear: function(parent) {
			parent.innerHTML = ""
		}
	};
	var reRegister = {
		init: function() {
			this.initNode();
			this.initEvent()
		},
		initNode: function() {
			this.reg = e._$get("reRegister")
		},
		initEvent: function() {
			v._$addEvent(this.reg, "click", this.onClickReg._$bind(this))
		},
		onClickReg: function() {
			if (e._$hasClassName(this.reg, "z-dis")) {
				return
			}
			e._$addClassName(this.reg, "z-dis");
			du.showHint({
				message: "正在转向注册页面...",
				autoDisappear: false
			});
			j._$requestByREST("/rest/account/reRegister/" + PID, {
				method: "DELETE",
				onload: this.cbReg._$bind(this),
				onerror: this.cbReg._$bind(this)
			})
		},
		cbReg: function(json) {
			if (json && json.code == 200 && json.result) {
				location.href = "/register"
			} else {
				du.showError(json && json.message || "跳转到注册页面失败，请稍后刷新重试！");
				e._$delClassName(this.reg, "z-dis")
			}
		}
	};
	if (e._$get("contact-statistic")) {
		dataViewer.init()
	}
	if (e._$get("reRegister")) {
		reRegister.init()
	}
})()